package com.ychong.library.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.os.Build
import android.text.TextPaint
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatButton
import com.ychong.library.R
import com.ychong.library.utils.ResUtils

class YCHButton : AppCompatButton {
    private var normalBgColor: Int? = null
    private var pressedBgColor: Int? = null
    private var enabledBgColor: Int? = null
    private var normalTextColor: Int? = null
    private var pressedTextColor: Int? = null
    private var enabledTextColor: Int? = null
    private var radiusSize: Float = 0f

    private var paint: Paint? = null

    private var textPain: TextPaint? = null

    private var showBgColor: Int? = null
    private var showTextColor:Int?=null

    companion object{
        const val BORDER_WIDTH:Float = 2f
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)


    @SuppressLint("Recycle", "ResourceType")
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.YCHButton)
        normalBgColor = typedArray.getColor(
            R.styleable.YCHButton_ychNormalBgColor,
            ResUtils.getColor(context, R.color.color_0D71DF)
        )
        pressedBgColor = typedArray.getColor(
            R.styleable.YCHButton_ychPressedBgColor,
            ResUtils.getColor(context, R.color.color_666666)
        )
        enabledBgColor = typedArray.getColor(
            R.styleable.YCHButton_ychEnabledBgColor,
            ResUtils.getColor(context, R.color.color_999999)
        )
        normalTextColor = typedArray.getColor(
            R.styleable.YCHButton_ychNormalTextColor,
            ResUtils.getColor(context, R.color.color_white)
        )
        pressedTextColor = typedArray.getColor(
            R.styleable.YCHButton_ychPressedTextColor,
            ResUtils.getColor(context, R.color.color_666666)
        )
        enabledTextColor = typedArray.getColor(
            R.styleable.YCHButton_ychEnabledTextColor,
            ResUtils.getColor(context, R.color.color_white)
        )
        radiusSize = typedArray.getDimension(R.styleable.YCHButton_ychBorderRadiusSize, 0f)
        typedArray.recycle()
        init()
    }

    private fun init() {
        paint = Paint()
        showBgColor = if (isEnabled) {
            normalBgColor!!
        } else {
            enabledBgColor!!
        }
        textPain = TextPaint()
        textPain!!.textSize = textSize
        showTextColor = if (isEnabled) {
            normalTextColor!!
        } else {
            enabledTextColor!!
        }

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //背景颜色
        paint!!.color = showBgColor!!
        if (radiusSize==0f){
            canvas!!.drawRect(0f, 0f, width * 1f, height * 1f, paint!!)
        }else{
            paint!!.strokeWidth = BORDER_WIDTH
            val halfBorderWidth = BORDER_WIDTH /2
            if (Build.VERSION.SDK_INT>=21){
                canvas!!.drawRoundRect(halfBorderWidth,halfBorderWidth,width*1f-halfBorderWidth,height*1f-halfBorderWidth,radiusSize,radiusSize,paint!!)
            }else{
                val rectF:RectF = RectF(halfBorderWidth,halfBorderWidth,width*1f-halfBorderWidth,height*1f-halfBorderWidth)
                canvas!!.drawRoundRect(rectF,radiusSize,radiusSize,paint!!)
            }
        }

        //文本颜色
        val textStr = text.toString()
        textPain!!.color = showTextColor!!
        val textWidth = textPain!!.measureText(textStr)
        val pfm: Paint.FontMetrics = textPain!!.fontMetrics
        val textHeight = pfm.ascent+pfm.descent
        canvas.drawText(textStr,(width-textWidth)/2f,(height-textHeight)/2f,textPain!!)


    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (isEnabled){
            when(event!!.action){
                MotionEvent.ACTION_DOWN ->{
                    //按下
                    showBgColor = pressedBgColor
                    showTextColor = pressedTextColor
                    invalidate()
                }
                MotionEvent.ACTION_UP,MotionEvent.ACTION_CANCEL ->{
                    //松开
                    showBgColor = normalBgColor
                    showTextColor = normalTextColor
                    invalidate()
                }
            }
        }
        return super.onTouchEvent(event)
    }
}